package com.qc.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qc.chat.ChatListMember;
import com.qc.common.DateUtil;
import com.qc.common.SMSClient;
import com.qc.common.Session;
import com.qc.common.StringUtil;
import com.qc.common.Util;
import com.qc.entity.Authentication;
import com.qc.entity.BannerEntity;
import com.qc.entity.BossIndustryEntity;
import com.qc.entity.BossJobEntity;
import com.qc.entity.EducationEntity;
import com.qc.entity.FootprintEntity;
import com.qc.entity.HeightEntity;
import com.qc.entity.InterestUserEntity;
import com.qc.entity.OccupationEntity;
import com.qc.entity.OpenCityEntity;
import com.qc.entity.OrderCommentEntity;
import com.qc.entity.PhoneVersionEntity;
import com.qc.entity.QiniuTokenEntity;
import com.qc.entity.ReportEntity;
import com.qc.entity.SecScheduleCommentEntity;
import com.qc.entity.SecScheduleReplyCommentEntity;
import com.qc.entity.SecretaryEntity;
import com.qc.entity.SecretaryVideoEntity;
import com.qc.entity.SkillTagEntity;
import com.qc.entity.TemperamentEntity;
import com.qc.entity.UserAuthVerifyEntity;
import com.qc.entity.UserBossEntity;
import com.qc.entity.UserBossScheduleEntity;
import com.qc.entity.UserCashEntity;
import com.qc.entity.UserEntity;
import com.qc.entity.UserFeedBackEntity;
import com.qc.entity.UserSecScheduleEntity;
import com.qc.entity.UserSecretaryEntity;
import com.qc.entity.WeiXinEntity;
import com.qc.entity.message.ChatMessageEntity;
import com.qc.entity.search.UserBossSearchEntity;
import com.qc.entity.search.UserSecSearchEntity;
import com.qc.jpush.JPushUtil;
import com.qc.serverlistener.CacheUtil;
import com.qc.service.AuthenticationService;
import com.qc.service.BossIndustryService;
import com.qc.service.BossJobService;
import com.qc.service.FootprintService;
import com.qc.service.OpenCityService;
import com.qc.service.TemperamentService;
import com.qc.service.UserService;
import com.qc.service.WeiXinService;
import com.qiniu.util.TokenUtil;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

@Controller
public class UserController extends BaseController {

	@Resource
	private UserService userService;
	
	@Resource
	private BossJobService bossService;
	
	@Resource
	private BossIndustryService industryService;
	
	@Resource
	private WeiXinService weixinService;
	
	@Resource(name="temperamentService")
	private TemperamentService temperamentService;
	
	@Resource(name="footprintService")
	private FootprintService footprintService;
	
	@Resource(name="authenticationService")
	private AuthenticationService authenticationService;
	
	@Resource(name="openCityService")
	private OpenCityService openCityService;
	
	/**
	 * 获取用户信息 http://localhost:8080/69mark/getUserInfo
	 */
	@RequestMapping(value = "/getUserInfo", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public void getUserInfo(@RequestParam("p1") String p1, HttpServletRequest request, HttpServletResponse response,
			Model model) {

		// String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p21345");
		System.out.println("p1:" + p1 + "===p2:" + p2);

		// return str;
	}

	

	@RequestMapping(value = "/MobileUserForgetPwd", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileUserForgetPwd(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");//"{\"mobile\":\"15618881550\",\"userPwd\":\"111222\",\"code\":\"123456\"}";
		System.out.println(p1);
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class);
		String code = userService.getMobileCode(entity.getMobile());
		if (code == null || !code.equals(entity.getCode())) {
			return returnFailedResult("验证码不正确");
		}
		
		long userId = userService.forgetPwd(entity.getMobile(), entity.getUserPwd());
		if (userId > 0) {
			return returnSuccessResult("修改成功");
		}
		return returnFailedResult("修改失败");
	}
	
	@RequestMapping(value = "/MobileUserLogin", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileUserLogin(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");//"{\"userName\":\"15618881550\",\"userPwd\":\"789123\"}";//
		
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class);
		
		UserEntity entity1 = userService.userLogin(entity.getUserName(),entity.getUserPwd());
		
		if (entity1 != null) {
			
			entity1.setToken(Session.getMobileToken(entity1.getUserId()));
			return returnSuccessResult(entity1);
		}
		
		return returnFailedResult("用户名或密码错误");
	}
	
	/**
	 * 根据token获取用户信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetUserInfoByToken", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetUserInfoByToken(HttpServletRequest request, HttpServletResponse response) {

		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserEntity entity = userService.getUserInfoByUserId(userId);
		
		return returnSuccessResult(entity);
	}
	
	/**
	 * 获取用户信息 http://localhost:8080/69mark/MobileSendCode
	 */
	@RequestMapping(value = "/MobileSendCode", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileSendCode(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");//"{\"mobile\":\"15618881550\"}"
		
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class);
		
		int code = Util.get6code();
		
		Boolean flag = SMSClient.send(entity.getMobile(), "您的注册验证码是"+code+"，为了保护您的账户安全，验证短信请勿转发给其他人。【圈城】");
		
		if (flag) {
			Util.logYXInfo("手机号:"+entity.getMobile()+"正在注册!");
			//先查询，如果存在就修改，如果不存在就保存
			
			String codeStr = userService.getMobileCode(entity.getMobile());
			if (codeStr != null && !codeStr.equals("")) {
				userService.updateCodeByMobile(entity.getMobile(), code+"");
			} else {
				userService.saveCode(entity.getMobile(), code+"");
			}
			return returnSuccessResult("发送验证码成功");
		}

		return returnFailedResult("验证码发送失败,稍后再试");
	}
	
	/**
	 * 用户登录 注册接口
	 * @param request
	 * @param response
	 * @return
	 * @throws SQLException 
	 */
	@RequestMapping(value = "/MobileUserLongin", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileUserLongin(HttpServletRequest request, HttpServletResponse response) throws SQLException {
		String p1 = request.getParameter("p1");
		
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class);
		
		String code = userService.getMobileCode(entity.getMobile());
		if (code == null || !code.equals(entity.getCode())) {
			return returnFailedResult("验证码不正确");
		}
		
		UserEntity userEntity = userService.getUserName(entity.getUserName());
		//验证手机号是否存在
		if (StringUtil.isNotEmpty(userEntity.getUserName())) {
			
			//userEntity.setToken(Session.getMobileToken(userEntity.getUserId()));
			
			return returnSuccessResult(userEntity);
			
		}else {
			
			entity.setCreatedTime(DateUtil.getCurrentSysTimestamp());
			entity.setNickname(Util.get8Code()+"");
			entity.setUserPwd("quancheng2017");
			
			long state = userService.register(entity);
			
			String token = Session.getMobileToken(state);
			entity.setToken(token);
			entity.setUserId(state);
			//处理内存
			CacheUtil.addUserInfo(entity);
			//保存token到数据库
			userService.saveTokenByUserId(state,token);
			
			//用客服默认给他发送一个消息
			ChatMessageEntity messageEntity = new ChatMessageEntity();

			messageEntity.setSenderId(1);//客服定死为1
			messageEntity.setContent("欢迎来到圈城，您有任何疑问都可以发消息给我哦!");
			messageEntity.setSendTime(DateUtil.getSysTimeout());
			messageEntity.setContentType(0);
			messageEntity.setSenderAvatar("");
			messageEntity.setSenderNickname("圈城客服");
			messageEntity.setCategory(3);
			
			messageEntity.setTargetId(state);
			//设置msg_id
			messageEntity.setMsgId(DateUtil.getSysTimestamp());
			ChatListMember.defaultSenderMessage(messageEntity);
			
			return returnSuccessResult(entity);
			
		}
		
	}
	
	
	@RequestMapping(value = "/MobileUserRegister", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileUserRegister(HttpServletRequest request, HttpServletResponse response) {
//		String strVal = Session.getMobileToken(9923l);
//		long userId = Session.getUserIdfromMobileSession(strVal);
//		Util.logYXInfo("YX测试日志");
//		Util.logExceptionINFO("代码异常情况出现的日志a");
		String p1 = request.getParameter("p1");//"{\"refSource\":\"iPhone6Plus\",\"deviceId\":\"F3E7F29E-1742-4F00-B824-244B820EA299\",\"userName\":\"15364646\",\"userPwd\":\"hfhfh\",\"mobile\":\"12345672342\"}";//
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class); 
		
		String code = userService.getMobileCode(entity.getMobile());
		if (code == null || !code.equals(entity.getCode())) {
			return returnFailedResult("验证码不正确");
		}
		
		entity.setCreatedTime(DateUtil.getCurrentSysTimestamp());
		entity.setNickname(Util.get8Code()+"");
		try {
			long state = userService.register(entity);
			String token = Session.getMobileToken(state);
			entity.setToken(token);
			entity.setUserId(state);
			//处理内存
			CacheUtil.addUserInfo(entity);
			//保存token到数据库
			userService.saveTokenByUserId(state,token);
			
			//用客服默认给他发送一个消息
			ChatMessageEntity messageEntity = new ChatMessageEntity();

			messageEntity.setSenderId(1);//客服定死为1
			messageEntity.setContent("欢迎来到圈城，您有任何疑问都可以发消息给我哦!");
			messageEntity.setSendTime(DateUtil.getSysTimeout());
			messageEntity.setContentType(0);
			messageEntity.setSenderAvatar("");
			messageEntity.setSenderNickname("圈城客服");
			messageEntity.setCategory(3);
			
			messageEntity.setTargetId(state);
			//设置msg_id
			messageEntity.setMsgId(DateUtil.getSysTimestamp());
			ChatListMember.defaultSenderMessage(messageEntity);
			
			return returnSuccessResult(entity);
		} catch (SQLException e) {
			if(e.getMessage().contains("UNIQUE")){
			    return returnFailedResult("手机号已被注册");
			}
		}

		return returnFailedResult("");
	}
	
	@RequestMapping(value = "/MobileUserSelRole", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileUserSelRole(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class); 
		
		long state = userService.updateRole(entity.getUserRole(), userId);
		if (state > 0) {
			return returnSuccessResult("");
		}

		return returnFailedResult("");
	}
	
	@RequestMapping(value = "/MobileGetQiniuUpToken", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetQiniuUpToken(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		QiniuTokenEntity tokenEntity = (QiniuTokenEntity) JSONObject.toBean(JSONObject.fromObject(p1), QiniuTokenEntity.class); 
		
		if (tokenEntity.getType() == 3) {
			// 图片上传token
			tokenEntity.setUploadToken(TokenUtil.imageUploadToken());
		} else if (tokenEntity.getType() == 5) {
			// 视频上传token
			tokenEntity.setUploadToken(TokenUtil.videoUploadToken());
		}

		return returnSuccessResult(tokenEntity);
	}
	
	@RequestMapping(value = "/MobileSaveAvatar", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileSaveAvatar(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class); 
		
		long state = userService.updateAvatar(entity.getAvatar(), userId);
		entity.setUserId(userId);
		if (state > 0) {
			//修改内存
			CacheUtil.updateUserInfo(entity);
			return returnSuccessResult("上传成功");
		}
		
		return returnFailedResult("上传失败");
	}
	
	@RequestMapping(value = "/MobileModifyUserInfo", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileModifyUserInfo(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class); 
		entity.setUserId(userId);
		try{
			long state = userService.updateUserInfo(entity);
			if (state > 0) {
				//修改内存
				CacheUtil.updateUserInfo(entity);
				return returnSuccessResult("修改成功");
			}
		} catch (SQLException e) {
			if(e.getMessage().contains("UNIQUE")){
			    return returnFailedResult("昵称已被使用");
			}
		}
		
		
		return returnFailedResult("修改失败");
	}
	
	/**
	 * 修改手机号
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileModifyBindMobile", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileModifyBindMobile(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class); 
		
		String code = userService.getMobileCode(entity.getMobile());
		if (code == null || !code.equals(entity.getCode())) {
			return returnFailedResult("验证码不正确");
		}
		
		try {
			long state = userService.updateBindMobile(entity.getMobile(), userId);
			if (state > 0) {
				//修改内存
				CacheUtil.updateUserInfo(entity);
				return returnSuccessResult("");
			}
		} catch (SQLException e) {
			if(e.getMessage().contains("UNIQUE")){
			    return returnFailedResult("此手机号已被使用");
			}
		}
		

		return returnFailedResult("");
	}
	
	/**
	 * 修改密码
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileModifyPwd", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileModifyPwd(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class); 
		entity.setUserId(userId);
		
		//1,根据旧的密码和userId查询
		UserEntity entity1 = userService.getUserInfoByPwdAndUserId(userId, entity.getUserPwd());
		
		if (entity1 == null) {
			return returnFailedResult("旧密码不正确");
		}
		
		//2,旧密码正确 再把新密码修改
		long state = userService.updateUserPwd(entity);
		if (state > 0) {
			return returnSuccessResult("密码修改成功");
		}

		return returnFailedResult("密码修改失败");
	}
	
	/**
	 * 意见反馈
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileUserFeedBack", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileUserFeedBack(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserFeedBackEntity entity = (UserFeedBackEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserFeedBackEntity.class); 
		entity.setUserId(userId);
		
		long state = userService.saveFeedBack(entity);
		if (state > 0) {
			return returnSuccessResult("保存成功");
		}

		return returnFailedResult("保存失败");
	}
	
	/**
	 * 秘书发布
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileAddSecretarySchedule", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileAddSecretarySchedule(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserSecScheduleEntity entity = (UserSecScheduleEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserSecScheduleEntity.class); 
		entity.setUserId(userId);
		entity.setCreatedTime(DateUtil.getCurrentSysTimestamp());
		
		long state = userService.saveSecretarySchedule(entity);
		if (state > 0) {
			return returnSuccessResult("发布成功");
		}

		return returnFailedResult("发布失败");
	}
	
	
	/**
	 * 秘书发布删除 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileDeleteSecretarySchedule", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileDeleteSecretarySchedule(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserSecScheduleEntity entity = (UserSecScheduleEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserSecScheduleEntity.class); 
		
		entity.setUserId(userId);
		entity.setSsId(entity.getSsId());
		
		int state = userService.deleteSecretarySchedule(entity);
		if (state > 0) {
			return returnSuccessResult("删除成功");
		}

		return returnFailedResult("删除失败");
	}
	
	/**
	 * 秘书获取自己发布的纪录
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetMySecSchedule", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetMySecSchedule(HttpServletRequest request, HttpServletResponse response) {

//		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		List<UserSecScheduleEntity> list = userService.getSecScheduleByUserId(userId);

		return returnSuccessResult(list);
	}
	
	/**
	 * 根据用户ID获取所有分享生活
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetSecScheduleByUserId", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetSecScheduleByUserId(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		
		UserSecScheduleEntity entity= (UserSecScheduleEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserSecScheduleEntity.class);
		
		List<UserSecScheduleEntity> list = userService.getSecScheduleByUserId(entity.getUserId());

		return returnSuccessResult(list);
	}
	
	/**
	 * 获取所有秘书技能
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetAllSkillTag", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetAllSkillTag(HttpServletRequest request, HttpServletResponse response) {

//		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		List<SkillTagEntity> list = userService.getAllSkillTag();

		return returnSuccessResult(list);
	}
	
	/**
	 * 根据城市获取秘书
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetSecScheduleByCity", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetSecScheduleByCity(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
//		String p2 = request.getParameter("p2");
//		long userId = Session.getUserIdfromMobileSession(p2);
//		if (userId == 0)
//			return returnFailedResult("");
		
		UserSecSearchEntity entity = (UserSecSearchEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserSecSearchEntity.class);
//		entity.setUserId(userId);
		//如果城市为null，说明用户拒绝获取定位
		List<UserSecScheduleEntity> list = userService.getSecScheduleByCity(entity);

		return returnSuccessResult(list);
	}
	
	/**
	 * 浏览统计接口
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileAddSecScheduleRead", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileAddSecScheduleRead(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
//		String p2 = request.getParameter("p2");
//		long userId = Session.getUserIdfromMobileSession(p2);
//		if (userId == 0)
//			return returnFailedResult("");
		
		UserSecScheduleEntity entity = (UserSecScheduleEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserSecScheduleEntity.class);
		//如果城市为null，说明用户拒绝获取定位
		long state = userService.updateSecScheduleRead(entity.getSsId());

		if (state > 0) {
			return returnSuccessResult("统计成功");
		}
		return returnFailedResult("统计失败");
	}
	
	/**
	 * 添加秘书发布的评论
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileAddSecScheduleComment", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileAddSecScheduleComment(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		SecScheduleCommentEntity entity = (SecScheduleCommentEntity) JSONObject.toBean(JSONObject.fromObject(p1), SecScheduleCommentEntity.class);
		entity.setUserId(userId);
		long state = userService.saveSecScheduleComment(entity);
		entity.setScId(state);
		if (state > 0) {
			//评论数加一
			userService.updateCommentCountPlus(entity.getSecScheduleId());
			
			return returnSuccessResult(entity);
		}
		return returnFailedResult("评论失败");
	}
	
	/**
	 * 回复评论
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileAddSecScheduleReplyComment", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileAddSecScheduleReplyComment(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		SecScheduleReplyCommentEntity entity = (SecScheduleReplyCommentEntity) JSONObject.toBean(JSONObject.fromObject(p1), SecScheduleReplyCommentEntity.class);
		entity.setUserId(userId);
		long state = userService.saveSecScheduleReplyComment(entity);

		if (state > 0) {
			return returnSuccessResult("评论成功");
		}
		return returnFailedResult("评论失败");
	}
	
	/**
	 * 根据秘书发布的ID查询评论及回复
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetSecretaryCommentBySSID", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetSecretaryCommentBySSID(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		SecScheduleCommentEntity entity = (SecScheduleCommentEntity) JSONObject.toBean(JSONObject.fromObject(p1), SecScheduleCommentEntity.class);
		
		List<SecScheduleCommentEntity> list = userService.getSecretaryCommentBySSID(entity.getSecScheduleId());

		return returnSuccessResult(list);
	}
	
	/**
	 * 删除评论
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileDelSecScheduleComment", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileDelSecScheduleComment(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		SecScheduleCommentEntity entity = (SecScheduleCommentEntity) JSONObject.toBean(JSONObject.fromObject(p1), SecScheduleCommentEntity.class);
		entity.setUserId(userId);
		long state = userService.delSecScheduleComment(entity);

		if (state > 0) {
			
			//评论数减一
			userService.updateCommentCountReduce(entity.getSecScheduleId());
			
			return returnSuccessResult("删除成功");
		}
		return returnFailedResult("删除失败");
	}
	
	/**
	 * 修改秘书微主页信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileModifySecretaryInfo", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileModifySecretaryInfo(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserSecretaryEntity entity = (UserSecretaryEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserSecretaryEntity.class);
		entity.setUserId(userId);
		long state = userService.updateSecretaryInfo(entity);
		if (state > 0) {
			return returnSuccessResult("编辑成功");
		}
		return returnFailedResult("编辑失败");
	}
	
	/**
	 * 获取秘书微主页信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetSecretaryInfoByUserId", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetSecretaryInfoByUserId(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
//		if (userId == 0)
//			return returnFailedResult("");
		
		UserSecretaryEntity entity = (UserSecretaryEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserSecretaryEntity.class);
		
		entity = userService.getSecretaryInfoByUserId(userId,entity.getSecUserId());

		return returnSuccessResult(entity);
	}
	
	/**
	 * 修改BOSS微主页信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileModifyBossInfo", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileModifyBossInfo(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserBossEntity entity = (UserBossEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserBossEntity.class);
		entity.setUserId(userId);
		long state = userService.updateBossInfo(entity);
		if (state > 0) {
			return returnSuccessResult("编辑boss信息成功");
		}
		return returnFailedResult("编辑boss信息失败");
	}
	
	/**
	 * 获取BOSS微主页信息
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetBossInfoByUserId", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetBossInfoByUserId(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserBossEntity entity = (UserBossEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserBossEntity.class);
		
		entity = userService.getBossInfoByUserId(userId,entity.getBossUserId());

		return returnSuccessResult(entity);
	}
	
	/**
	 * BOSS发布行程发布
	 */
	@RequestMapping(value = "/MobileAddBossSchedule", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileAddBossSchedule(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserBossScheduleEntity entity = (UserBossScheduleEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserBossScheduleEntity.class);
		entity.setUserId(userId);
		long state = userService.saveBossSchedule(entity);
		if (state > 0) {
			return returnSuccessResult("行程发布成功");
		}
		return returnFailedResult("行程发布失败");
	}
	
	/**
	 * BOSS获取自己发布的纪录
	 */
	@RequestMapping(value = "/MobileGetMyBossSchedule", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetMyBossSchedule(HttpServletRequest request, HttpServletResponse response) {

//		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		List<UserBossScheduleEntity> list = userService.getBossScheduleByUserId(userId);

		return returnSuccessResult(list);
	}
	
	/**
	 * 根据城市获取BOSS
	 */
	@RequestMapping(value = "/MobileGetBossScheduleByCity", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetBossScheduleByCity(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserBossSearchEntity entity = (UserBossSearchEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserBossSearchEntity.class);
		entity.setUserId(userId);
		List<UserBossScheduleEntity> list = userService.getBossScheduleByCity(entity);

		return returnSuccessResult(list);
	}
	
	@RequestMapping(value = "/MobileModifyRegistrationId", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileModifyRegistrationId(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class); 
		
		long state = userService.updateRegistrationId(entity.getRegistrationId(), userId);
		//修改缓存用户信息
		CacheUtil.updateRegId(entity.getRegistrationId(), userId);
		if (state > 0) {
			return returnSuccessResult("保存成功");
		}

		return returnFailedResult("保存失败");
	}
	
	@RequestMapping(value = "/MobileAddInterestUser", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileAddInterestUser(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		InterestUserEntity entity = (InterestUserEntity) JSONObject.toBean(JSONObject.fromObject(p1), InterestUserEntity.class); 
		if (entity.getSenderUserId() == entity.getReceiveUserId()) {
			return returnFailedResult("不能关注自己");
		}
		long state = userService.interestUser(entity);
		if (state > 0) {
			//给被访问者发推送
			JPushUtil.redDotPush(4, entity.getReceiveUserId(), "有新的粉丝,点击查看!");
			return returnSuccessResult("关注成功");
		}

		return returnFailedResult("关注失败");
	}
	
	@RequestMapping(value = "/MobileCancelInterestUser", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileCancelInterestUser(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		InterestUserEntity entity = (InterestUserEntity) JSONObject.toBean(JSONObject.fromObject(p1), InterestUserEntity.class); 
		
		long state = userService.cancelInterestUser(entity);
		if (state > 0) {
			return returnSuccessResult("取消成功");
		}

		return returnFailedResult("取消失败");
	}
	
	/**
	 * 查看感兴趣的人
	 * 参数p1 : 1/表示查询BOSS  2/表示查询秘书
	 */
	@RequestMapping(value = "/MobileGetInterestUser", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetInterestUser(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		List<InterestUserEntity> list = userService.getInterestUser(userId,Integer.parseInt(p1));

		return returnSuccessResult(list);
	}
	
	/**
	 * 获取所有订单评价根据userId
	 */
	@RequestMapping(value = "/MobileGetOrderCommentByUserId", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetOrderCommentByUserId(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		OrderCommentEntity entity = (OrderCommentEntity) JSONObject.toBean(JSONObject.fromObject(p1), OrderCommentEntity.class);
		
		List<OrderCommentEntity> list = userService.getOrderCommentByUserId(entity.getReceiveUserId());

		return returnSuccessResult(list);
	}
	
	/**
	 * 根据秘书发布的SSID获取详情(包含评论数量，发布人的头像昵称，星级，日薪)
	 */
	@RequestMapping(value = "/MobileGetSecScheduleBySsId", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetSecScheduleBySsId(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		
		UserSecScheduleEntity entity = (UserSecScheduleEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserSecScheduleEntity.class);
		
		List<FootprintEntity> listFootprint = footprintService.listAlFootprint(entity.getSsId(), 0, 1, 10);
		
		entity = userService.getSecScheduleBySSId(entity.getSsId(), userId);
		
		int pointState = userService.getPointCount(entity.getSsId(), userId);
		
		entity.setListFootprint(listFootprint);
		entity.setPointState(pointState);
		
		return returnSuccessResult(entity);
	}
	
	/**
	 * 跳转到分享页
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/toUrlIntro2",produces = "text/html;charset=UTF-8")
	public ModelAndView toUrlIntro2(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		ModelAndView mav = new ModelAndView("/intro");
		
        return mav;
	}
	
	/**
	 * 微信分享 数据展示
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/toUrlIntro", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
	public String toUrlIntro(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String ssId = request.getParameter("ssId");
		String url = request.getParameter("url");
		long userId = Long.parseLong(request.getParameter("userId"));
		
		//3、时间戳和随机字符串  
        String noncestr = UUID.randomUUID().toString().replace("-", "").substring(0, 16);//随机字符串
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳
        
        List<FootprintEntity> listFootprint = footprintService.listAlFootprint(Long.parseLong(ssId), 0, 1, 10);
		
        UserSecScheduleEntity entity = userService.getUserSecScheduleId(ssId);
        
        int pointState = userService.getPointCount(entity.getSsId(), userId);
		
		entity.setPointState(pointState);
        
        if (0 == entity.getSsId()) {
        	return "2";
        }
        
        long time = System.currentTimeMillis();
        
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String time2 = sf.format(time);
        
        entity.setTime(DateUtil.getDateDays(DateUtil.stringFormatDate(time2), DateUtil.stringFormatDate(entity.getCreatedTime().substring(0,10))));
		
		entity.setListFootprint(listFootprint);
		
		String eImage = "";
		String[] b = entity.getSsContent().split("~qc~");
        String content = "";
        String content2 = "";
        for (String c : b) {
            String[] d = c.split("&qc&");
            
            for (int i = 0 ; i<d.length;i++) {
            	if (i == 0) {
            		content += "<p style='margin-top: 3%;'>"+d[i]+"</p>";
                    
                    content2 += d[i];
            	}else {
            		 String name = "http://qiniu.images.69mark.com/"+d[i];
                     
                	 eImage = "<img style='width: 100%;' src="+name+">";
                	 content += eImage;
            	}
            }
            
            /*if (d.length>1) {
            	if (d[0] != null) {
                    content += "<p style='margin-top: 3%;'>"+d[0]+"</p>";
                    
                    content2 += d[0];
                }
                
                if (d[1] != null) {
                	 String name = "http://qiniu.images.69mark.com/"+d[1];
                    
                	 eImage = "<img style='width: 100%;' src="+name+">";
                	 content += eImage;
                }
            }else {
            	if (d[0] != null) {
                    content += "<p class=\"font3\">"+d[0]+"</p>";
                    
                    content2 += d[0];
                }
            }*/
        }
        entity.setSsContent(content);
		
        WeiXinEntity weiXinEntity = weixinService.getWeiXinEntity();
        if (StringUtil.isNotEmpty(weiXinEntity.getAccessToken())) {
        	
        	try {
				if (WeiXin.time(DateUtil.stringFormat(weiXinEntity.getModifiedTime())) == false) {
					
					JSONObject json = WeiXin.getAccessToken(weiXinEntity);
	                String access_token = json.getString("access_token");
	                
	                weiXinEntity.setAccessToken(access_token);
	                
	                String urls = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+weiXinEntity.getAccessToken()+"&type=jsapi";
	        		JSONObject jsonObject1 = WeiXin.httpRequest(urls, "GET", new JSONObject());
	        		String jsapi_ticket = jsonObject1.getString("ticket");
	        		
	        		weiXinEntity.setJsapiTicket(jsapi_ticket);
	        		weiXinEntity.setNoncestr(noncestr);
	        		weiXinEntity.setTimestamp(timestamp);
	                weixinService.updateWeiXinId(weiXinEntity);

				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
        	
        }else {
        	//获取token
        	JSONObject json = WeiXin.getAccessToken(weiXinEntity);
            String access_token = json.getString("access_token");
            String appId = json.getString("appId");
            
            weiXinEntity.setAccessToken(access_token);

            //获取密钥
            String urls = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+weiXinEntity.getAccessToken()+"&type=jsapi";
    		JSONObject jsonObject1 = WeiXin.httpRequest(urls, "GET", new JSONObject());
    		String jsapi_ticket = jsonObject1.getString("ticket");

    		weiXinEntity.setJsapiTicket(jsapi_ticket);
    		weiXinEntity.setAppId(appId);
    		weiXinEntity.setNoncestr(noncestr);
    		weiXinEntity.setTimestamp(timestamp);
    		
    		weixinService.saveWeiXin(weiXinEntity);
            
        }
        
        try {
            
        	// Create Hex String  
	        StringBuffer hexString = new StringBuffer(); 
            
            String str = "jsapi_ticket="+weiXinEntity.getJsapiTicket()+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+url; 
            
			try {  
				MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1");  
		        digest.update(str.getBytes());  
		        byte messageDigest[] = digest.digest();  
		        // 字节数组转换为 十六进制 数  
		            for (int i = 0; i < messageDigest.length; i++) {  
		                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);  
		                if (shaHex.length() < 2) {  
		                    hexString.append(0);  
		                }  
		                hexString.append(shaHex);  
		            }  
		            
		        } catch (NoSuchAlgorithmException e) {  
		            e.printStackTrace();  
		        }

			entity.setNoncestr(noncestr);
			entity.setTimestamp(timestamp);
			entity.setSignature(hexString.toString());
			entity.setAppId(weiXinEntity.getAppId());
			entity.setContent2(content2);
			
			System.out.println(url);
			System.out.println(noncestr);
			System.out.println(timestamp);
			System.out.println(hexString.toString());
			System.out.println(weiXinEntity.getAppId());
            
        } catch (JSONException e) {
        }
		return JSONObject.fromObject(entity).toString();

	}
	
	public static void writeImageToDisk(byte[] img, String fileName,String url){  
        try { 
        	
        	//String url = HttpServletRequest.getSession().getServletContext().getRealPath("/")+("/fileImage");
        	
            File file = new File(url + "/" + fileName);  
            FileOutputStream fops = new FileOutputStream(file);  
            fops.write(img);  
            fops.flush();  
            fops.close();  
            System.out.println("图片已经写入到C盘");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    } 
	
	public static byte[] getImageFromNetByUrl(String strUrl){  
        try {  
            URL url = new URL(strUrl);  
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
            conn.setRequestMethod("GET");  
            conn.setConnectTimeout(5 * 1000);  
            InputStream inStream = conn.getInputStream();//通过输入流获取图片数据  
            byte[] btImg = readInputStream(inStream);//得到图片的二进制数据  
            return btImg;  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
	
	public static byte[] readInputStream(InputStream inStream) throws Exception{  
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
        byte[] buffer = new byte[1024];  
        int len = 0;  
        while( (len=inStream.read(buffer)) != -1 ){  
            outStream.write(buffer, 0, len);  
        }  
        inStream.close();  
        return outStream.toByteArray();  
    } 
	
	
	/**
	 * 秘书微主页视频查看接口
	 */
	@RequestMapping(value = "/MobileGetSecretaryVideoByUserId", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetSecretaryVideoByUserId(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
//		String p2 = request.getParameter("p2");
//		long userId = Session.getUserIdfromMobileSession(p2);
//		if (userId == 0)
//			return returnFailedResult("");
		
		SecretaryVideoEntity entity = (SecretaryVideoEntity) JSONObject.toBean(JSONObject.fromObject(p1), SecretaryVideoEntity.class);
		
		entity = userService.getSecretaryVideoByUserId(entity.getUserId());

		return returnSuccessResult(entity);
	}
	
	/**
	 * 秘书微主页视频添加接口
	 */
	@RequestMapping(value = "/MobileAddSecretaryVideo", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileAddSecretaryVideo(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		SecretaryVideoEntity entity = (SecretaryVideoEntity) JSONObject.toBean(JSONObject.fromObject(p1), SecretaryVideoEntity.class);
		entity.setUserId(userId);
		long state = userService.addSecretaryVideo(entity);

		if (state > 0) {
			return returnSuccessResult("保存成功");
		}

		return returnFailedResult("保存失败");
	}
	
	/**
	 * 秘书微主页视频更改接口
	 */
	@RequestMapping(value = "/MobileModifySecretaryVideo", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileModifySecretaryVideo(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		SecretaryVideoEntity entity = (SecretaryVideoEntity) JSONObject.toBean(JSONObject.fromObject(p1), SecretaryVideoEntity.class);
		entity.setUserId(userId);
		long state = userService.updateSecretaryVideo(entity);

		if (state > 0) {
			return returnSuccessResult("更改成功");
		}

		return returnFailedResult("更改失败,请重试");
	}
	
	/**
	 * 微视频阅读量统计接口 
	 */
	@RequestMapping(value = "/MobileAddSecretaryVideoRead", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileAddSecretaryVideoRead(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
//		String p2 = request.getParameter("p2");
//		long userId = Session.getUserIdfromMobileSession(p2);
//		if (userId == 0)
//			return returnFailedResult("");
		
		SecretaryVideoEntity entity = (SecretaryVideoEntity) JSONObject.toBean(JSONObject.fromObject(p1), SecretaryVideoEntity.class);

		long state = userService.updateSecretaryVideoRead(entity.getUserId());

		if (state > 0) {
			return returnSuccessResult("统计成功");
		}

		return returnFailedResult("统计失败");
	}
	
	/**
	 * 会员认证信息提交接口
	 */
	@RequestMapping(value = "/MobileAddAuthVerify", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileAddAuthVerify(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserAuthVerifyEntity entity = (UserAuthVerifyEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserAuthVerifyEntity.class);
		entity.setUserId(userId);
		long state = userService.addAuthVerify(entity);

		if (state > 0) {
		    UserEntity userEntity = userService.getUserInfoByUserId(userId);
		    if (userEntity != null) {
		        //真是姓名 赋值给 user表
		        userEntity.setRealName(entity.getvRealName());
		        
		        try {
					userService.updateUserInfo(userEntity);
				} catch (SQLException e) {
				}
		    }
			return returnSuccessResult("提交成功");
		}

		return returnFailedResult("提交失败");
	}
	
	/**
	 * 会员认证信息查看接口
	 */
	@RequestMapping(value = "/MobileGetAuthVerifyByUserId", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetAuthVerifyByUserId(HttpServletRequest request, HttpServletResponse response) {

//		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserAuthVerifyEntity entity = userService.getAuthVerify(userId);

		return returnSuccessResult(entity);
	}
	
	/**
	 * 会员认证信息修改接口
	 */
	@RequestMapping(value = "/MobileModifyAuthVerify", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileModifyAuthVerify(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserAuthVerifyEntity entity = (UserAuthVerifyEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserAuthVerifyEntity.class);
		entity.setUserId(userId);
		long state = userService.updateAuthVerify(entity);

		if (state > 0) {
		    UserEntity userEntity = userService.getUserInfoByUserId(userId);
		    if (userEntity != null) {
		        //真是姓名 赋值给 user表
		        userEntity.setRealName(entity.getvRealName());
		        
		        try {
					userService.updateUserInfo(userEntity);
				} catch (SQLException e) {
				}
		    }
			return returnSuccessResult("提交成功");
		}

		return returnFailedResult("提交失败");
	}
	
	/**
	 * 获取所有身高数据(秘书微主页用)
	 */
	@RequestMapping(value = "/MobileGetAllHeight", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetAllHeight(HttpServletRequest request, HttpServletResponse response) {

//		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		List<HeightEntity> list = userService.getAllHeight();

		return returnSuccessResult(list);
	}
	
	/**
	 * 获取所有学历数据(秘书微主页用)
	 */
	@RequestMapping(value = "/MobileGetAllEducation", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetAllEducation(HttpServletRequest request, HttpServletResponse response) {

//		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		List<EducationEntity> list = userService.getAllEducation();

		return returnSuccessResult(list);
	}
	
	/**
	 * 获取所有职业数据(秘书微主页用)
	 */
	@RequestMapping(value = "/MobileGetAllOccupation", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetAllOccupation(HttpServletRequest request, HttpServletResponse response) {

//		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		List<OccupationEntity> list = userService.getAllOccupation();

		return returnSuccessResult(list);
	}
	
	/**
	 * 获取一级专业数据(秘书微主页用)
	 */
	@RequestMapping(value = "/MobileGetAllOneSpecialty", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetAllOneSpecialty(HttpServletRequest request, HttpServletResponse response) {

//		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		List<SecretaryEntity> list = userService.getAllOneSpecialty();

		return returnSuccessResult(list);
	}
	
	/**
	 * 获取一级专业数据(秘书微主页用)
	 */
	@RequestMapping(value = "/MobileGetAllTwoSpecialty", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetAllTwoSpecialty(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		SecretaryEntity entity = (SecretaryEntity) JSONObject.toBean(JSONObject.fromObject(p1), SecretaryEntity.class);
		
		List<SecretaryEntity> list = userService.getAllTwoSpecialty(entity.getParent());

		return returnSuccessResult(list);
	}
	
	/**
	 * 获取banner
	 */
	@RequestMapping(value = "/MobileGetBanner", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetBanner(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
//		String p2 = request.getParameter("p2");
//		long userId = Session.getUserIdfromMobileSession(p2);
//		if (userId == 0)
//			return returnFailedResult("");
		
		BannerEntity entity = (BannerEntity) JSONObject.toBean(JSONObject.fromObject(p1), BannerEntity.class);
		
		List<BannerEntity> list = userService.getBannerByType(entity);

		return returnSuccessResult(list);
	}
	
	/**
	 * 移动端提现 
	 */
	@RequestMapping(value = "/MobileAddCashOut", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileAddCashOut(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserCashEntity entity = (UserCashEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserCashEntity.class);

		//1，查看余额是否有这么多
		UserEntity uEntity = userService.getUserInfoByUserId(userId);
		
		if (uEntity.getBalance() < entity.getCashBalance()) {
			return returnFailedResult("提现失败,余额不足");
		}
		//2，计算提现后的余额
		entity.setAccountBalance(uEntity.getBalance() - entity.getCashBalance());
		
		entity.setUserId(userId);
		long state = userService.addCashOut(entity);
		
		
		if (state > 0) {
			//修改个人余额
			userService.updateUserBalance(entity.getAccountBalance(), userId);
			
			return returnSuccessResult(String.valueOf(entity.getAccountBalance()));
		}

		return returnFailedResult("申请提现失败");
	}
	
	/**
	 * 获取资金明细
	 */
	@RequestMapping(value = "/MobileGetCashDetail", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetCashDetail(HttpServletRequest request, HttpServletResponse response) {

//		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		List<UserCashEntity> list = userService.getUserCashDetail(userId);

		return returnSuccessResult(list);
	}
	
	/**
	 * 获取BOSS职务信息
	 */
	@RequestMapping(value = "/MobileGetAllBossJob", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetAllBoss(HttpServletRequest request, HttpServletResponse response) {
		
//		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		List<BossJobEntity> list = bossService.getAllBossJob();

		return returnSuccessResult(list);
	}
	
	/**
	 * 获取所属行业
	 */
	@RequestMapping(value = "/MobileGetAllBossIndustry", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetAllBossIndustry(HttpServletRequest request, HttpServletResponse response) {
		
//		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		List<BossIndustryEntity> list = industryService.getAllBossIndustry();

		return returnSuccessResult(list);
	}
	
	/**
	 * 获取客户端版本号，用户提示用户更新
	 */
	@RequestMapping(value = "/MobileGetPhoneVersion", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileGetPhoneVersion(HttpServletRequest request, HttpServletResponse response) {
		
		String p1 = request.getParameter("p1");
//		String p2 = request.getParameter("p2");
//		long userId = Session.getUserIdfromMobileSession(p2);
//		if (userId == 0)
//			return returnFailedResult("");
		
		PhoneVersionEntity entity = (PhoneVersionEntity) JSONObject.toBean(JSONObject.fromObject(p1), PhoneVersionEntity.class);
		
		entity = userService.getPhoneVersionByType(entity.getType());

		return returnSuccessResult(entity);
	}
	
	/**
	 * 用户截图举报
	 */
	@RequestMapping(value = "/MobileReportScreenshot", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String mobileReportScreenshot(HttpServletRequest request, HttpServletResponse response) {
		
		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		ReportEntity entity = (ReportEntity) JSONObject.toBean(JSONObject.fromObject(p1), ReportEntity.class);
		entity.setUserId(userId);
		long state = userService.reportScreenshot(entity);

		if (state > 0) {
			return returnSuccessResult("举报成功");
		}

		return returnFailedResult("举报失败");
	}
	
	/**
	 * 获取所有气质标签
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileTemperamentAll", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileTemperamentAll(HttpServletRequest request, HttpServletResponse response) {

		String p1 = request.getParameter("p1");
		TemperamentEntity temperamentEntity = (TemperamentEntity)JSONObject.toBean(JSONObject.fromObject(p1),TemperamentEntity.class);
		
		List<TemperamentEntity> list = temperamentService.listAllTemperament(temperamentEntity.getSex());
		
		return returnSuccessResult(list);
	}
	
	/**
	 * 是否接单状态开关
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileUpdateOrdersState", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileUpdateOrdersState(HttpServletRequest request, HttpServletResponse response) {
		
		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		UserEntity entity = (UserEntity) JSONObject.toBean(JSONObject.fromObject(p1), UserEntity.class);
		
		int flag = userService.updateOrdersState(userId, entity.getOrdersState());
		if (flag == 1) {
			return returnSuccessResult("修改成功");
		}else {
			return returnFailedResult("修改失败");
		}
		
	}
	
	/**
	 * 职业认证
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileSaveAuthentication", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileSaveAuthentication(HttpServletRequest request, HttpServletResponse response) {
		
		String p1 = request.getParameter("p1");
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		int flag = 0;
		
		Authentication entity = (Authentication) JSONObject.toBean(JSONObject.fromObject(p1), Authentication.class);
		
		Authentication entity2 = authenticationService.getAuthentication(String.valueOf(userId));
		
		if (entity2.getUserId() == 0) {
			
			entity2.setType(1);
			entity2.setUserId(userId);
			entity2.setCompany(entity.getCompany());
			entity2.setDescribes(entity.getDescribes());
			entity2.setPost(entity.getPost());
			entity2.setUploadImage(entity.getUploadImage());
			
			flag = authenticationService.saveAuthentication(entity2);
			
		}else {
			entity2.setType(1);
			entity2.setUploadImage(entity.getUploadImage());
			entity2.settId(entity2.gettId());
			
			flag = authenticationService.updateAuthenticationId(entity2);
			
		}
		
		if (flag == 1) {
			return returnSuccessResult("提交成功");
		}else {
			return returnFailedResult("修改失败");
		}
		
	}
	
	/**
	 * 商家查看
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileGetAuthenticationByUserId", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileGetAuthenticationByUserId(HttpServletRequest request, HttpServletResponse response) {
		
		String p2 = request.getParameter("p2");
		long userId = Session.getUserIdfromMobileSession(p2);
		if (userId == 0)
			return returnFailedResult("");
		
		Authentication entity = authenticationService.getAuthentication(String.valueOf(userId));
		
		return returnSuccessResult(entity);
	}
	
	/**
	 * 开通城市 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/MobileOpenCityAll", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String MobileOpenCityAll(HttpServletRequest request, HttpServletResponse response) {

		List<OpenCityEntity> list = openCityService.getAllOpenCity(1, 9999);
		
		return returnSuccessResult(list);
	}
	
}
